package com.ccsoft.backuptools.controller;

import com.ccsoft.backuptools.utils.DbUtils;
import com.ccsoft.backuptools.utils.ZipUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.AntPathMatcher;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.HandlerMapping;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

@CrossOrigin
@RestController
@RequestMapping("/db")
public class DbController {
    @Autowired
    private DbUtils dbUtils;

    @GetMapping("/backup/{dbname}")
    public String backup(@PathVariable String dbname){
        try {
            dbUtils.backupdb(dbname);
        } catch (Exception e){
            return "备份数据库" + dbname + "时出现异常！";
        }
        return "备份完毕！";
    }

    // 传输的参数：{"originalFilename":"D:\\autobackup\\PADAccount20220705145150.bak","zipFilename":"D:\\autobackup\\db.zip"}
    @PostMapping("/toZip")
    public String toZip(@RequestBody Map<String,String> params){
        String originalFilename = params.get("originalFilename");
        String zipFilename = params.get("zipFilename");

        try {
            ZipUtils.toZip(originalFilename,zipFilename);
        }catch (Exception e){
            return "压缩时出现异常！";
        }
        return "压缩完毕！";
    }
}
